package study.kit.function;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
public class GoalUtils {

    /**
     * ==============================
     * （★）反射（传递数据）
     * ==============================
     */
    public static String appendByData(List<Goal> list, String fieldName) {
        list.stream().collect(Collectors.groupingBy(Goal::getLevel));
        //
        List<String> nameLt = CollectionUtil.map(list, e -> {
            //
            Object value = ReflectUtil.getFieldValue(e, fieldName);
            String name = Convert.toStr(value);
            //
            if (StrUtil.equals("3", e.getLevel())) {
                return "初中-" + name;
            } else {
                return "高中-" + name;
            }
        }, true);
        //
        return StrUtil.join(StrUtil.COMMA, nameLt);
    }

    /**
     * ==============================
     * （★）函数（传递行为）
     * ==============================
     */
    public static String appendByAction(List<Goal> list, Function<Goal, String> action) {
        //
        List<String> nameLt = CollectionUtil.map(list, action, true);
        return StrUtil.join(StrUtil.COMMA, nameLt);
        //
//        return list.stream()
//                .map(action)
//                .collect(Collectors.joining(StrUtil.COMMA));
    }

    public static String addPrefix(String level, String name) {
        if (StrUtil.equals("3", level)) {
            return "初中-" + name;
        } else {
            return "高中-" + name;
        }
    }
}
